public class AccountTaskEventController
{
	public list<TaskEventWraper> listWperAddNewTask{get;set;}
	public string AccountId{get;set;}
	public AccountTaskEventController(ApexPages.StandardController stdController) 
	{
 		Account acc=(Account)stdController.getRecord();
 		AccountId=acc.Id;
 		Set<Id> setIds=new set<Id>();
 		Set<Id> setaccid=new Set<Id>();
 		listWperAddNewTask=new list<TaskEventWraper>();
 		List<Account> Lstaccount;
 		setIds.add(AccountId);
		Account masteracc=[select id,Master_Client__c,RecordType.Name from Account where id=:AccountId];
			if(masteracc.RecordType.Name=='AAS Sales Account')
			{
    			if(masteracc.Master_Client__c!=null)
    			{
    				Account Mstaccount=[Select id from Account where Id=:masteracc.Master_Client__c];       
    				setIds.add(Mstaccount.Id);
    				setaccid.add(AccountId);
    			}
			}
			else if(masteracc.RecordType.Name=='AAS Master Client')
			{
    			Lstaccount=[Select id from Account where Master_Client__c=:AccountId];
    			if(Lstaccount.size()!=0)
    			{
        			for(Account accnt:Lstaccount)
        			{
        				setaccid.add(accnt.Id);
        				setIds.add(accnt.Id);
        			}
    			}
    			else
    			{
    				setaccid.add(AccountId);
    			}
    
			}
		List<Opportunity> lstOpportunity=[Select id from Opportunity  where AccountId in:setaccid];
		for(Opportunity opp:lstOpportunity)
		{
			setIds.add(opp.Id);
		}
		List<Quote> LstQuote=[select Id from Quote where OpportunityId in :lstOpportunity];
		for(Quote Quote1:LstQuote)
		{
			setIds.add(Quote1.Id);
		}
		List<Order__c> lstOrder=[select id from Order__c where Opportunity__c in:lstOpportunity];
		for(Order__c Order:lstOrder)
		{
			setIds.add(Order.Id);
		}
		List<Task> LstTask=[Select ID,OwnerID,WhatID,Owner.Name,What.Type,What.Name,Subject,Status,Appointment_Type__c,ActivityDate From Task where WhatId in:setIds];
		for(Task tsk:LstTask)
		{
			TaskEventWraper taskenentwrp=new TaskEventWraper();
			taskenentwrp.Id=tsk.ID;
			taskenentwrp.Subject=tsk.Subject;
			taskenentwrp.Appointment=tsk.Appointment_Type__c;
			taskenentwrp.Duedate=String.valueof(tsk.ActivityDate);
			taskenentwrp.ParentId=tsk.WhatID;
			taskenentwrp.OwnerID=tsk.OwnerId;
			if(tsk.What.Type=='Order__c')
				taskenentwrp.Objects ='Fulfillment Order';
			else
				taskenentwrp.Objects=tsk.What.Type;
				taskenentwrp.RelatedTo=tsk.What.Name;
				taskenentwrp.Status=tsk.Status;
				taskenentwrp.AssignedTo=tsk.Owner.Name;
				listWperAddNewTask.add(taskenentwrp);
		}
		List<Event> LstEvent=[Select ID,OwnerID,Owner.Name,WhatId,What.Type,What.Name,Subject,StartDateTime,Appointment_Type__c From Event where WhatId in:setIds];
		for(Event evnt:LstEvent)
		{
			TaskEventWraper taskenentwrp=new TaskEventWraper();
			taskenentwrp.Id=evnt.ID;
			taskenentwrp.Subject=evnt.Subject;
			taskenentwrp.Appointment=evnt.Appointment_Type__c;
			taskenentwrp.Duedate=String.valueof(evnt.StartDateTime);
			taskenentwrp.ParentID=evnt.WhatId;
			taskenentwrp.OwnerID=evnt.OwnerId;
			if(evnt.What.Type=='Order__c')
				taskenentwrp.Objects ='Fulfillment Order';
			else
				taskenentwrp.Objects=evnt.What.Type;
			taskenentwrp.RelatedTo=evnt.What.Name;
			//taskenentwrp.Status=tsk.Status;
			taskenentwrp.AssignedTo=evnt.Owner.Name;
			listWperAddNewTask.add(taskenentwrp);
		}
	}

	public String getParam(String name) 
	{
		return ApexPages.currentPage().getParameters().get(name);   
	}
	public PageReference del() 
	{
    try 
    	{
			String delid = getParam('delid');
        	Integer rowNo =Integer.valueOf(getParam('rowNo'));     
        	listWperAddNewTask.remove(rowNo);    
        	if(delid.startsWith('00T'))
        	{
        		Task tsk=[select id from Task where id=:delid];
        		delete tsk;
        }
        else
        {
        	Event evnt=[select id from Event where id=:delid];
        	delete evnt;
        }
            
        } catch (Exception e) {
            ApexPages.addMessages(e);
        }
        return null;
	}
	public PageReference edit()
	{
        String editid = getParam('editid');
        PageReference pageref=new PageReference('/'+editid+'/e');
        pageref.setredirect(true);
        return pageref;
	}

}